package com.copa.gameStore.controller;

import com.copa.gameStore.application.UserApplicationService;
import com.copa.gameStore.domain.User;
import com.copa.gameStore.domain.UserAccount;
import com.copa.gameStore.dto.UserAccountFeignDto;
import com.copa.gameStore.dto.UserDto;
import com.copa.gameStore.vo.ResponseVo;
import com.copa.gameStore.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-10-20 10:54
 * @function
 */
@RestController
@Api(tags = "用户服务")
public class UserController{

    @Resource
    private UserApplicationService userApplicationService;

    @GetMapping("roleNameByUserId")
    @ApiOperation("此方法为feign调用，获取角色列表")
    public List<String> roleNameByUserId(@RequestParam("userId") Long userId) {
        return userApplicationService.roleNameByUserId(userId);
    }

    @GetMapping("findByUsername")
    @ApiOperation("此方法为feign调用，获取用户信息")
    public User findByUsername(@RequestParam("username") String username) {
        return userApplicationService.findByUsername(username);
    }

    @ApiOperation("用户注册")
    @PostMapping("register")
    public ResponseVo<User> register(@Validated @RequestBody UserDto userDto) {
        return userApplicationService.register(userDto);
    }

    @ApiOperation("用户修改个人信息")
    @PutMapping("update")
    //@Auth
    public ResponseVo<String> put(@Validated @RequestBody UserDto userDto) {
        return userApplicationService.put(userDto);
    }

    @ApiOperation("查询用户余额")
    @GetMapping("account")
    public ResponseVo<UserAccount> getAccount() {
        return userApplicationService.account();
    }

    @ApiOperation("充值")
    @PutMapping("recharge")
    public ResponseVo<String> recharge(@RequestParam("username") String username,
                                       @RequestParam("money") BigDecimal money) {
        return userApplicationService.recharge(username, money);
    }

    @ApiOperation("重置密码")
    @PutMapping("resetPassword")
    public ResponseVo<String> resetPassword(@RequestParam("username") String username) {
        return userApplicationService.resetPassword(username);
    }

    @ApiOperation("个人信息")
    @GetMapping("getUserInfo")
    public ResponseVo<UserVo> get() {
        return userApplicationService.get();
    }

    @ApiOperation("查询用户信息")
    @GetMapping("{id}")
    public ResponseVo<User> getUser(@PathVariable("id") Long id) {
        return userApplicationService.getUser(id);
    }

//    @ApiOperation("此方法为feign调用，获取用户信息")
//    @GetMapping("userInfo")
//    public ResponseVo<UserVo> userInfo(@RequestParam("authorization") String authorization) {
//        return ResponseVo.success(userApplicationService.userInfo(authorization));
//    }

    @ApiOperation("此方法为feign调用，对账号余额进行操作")
    @PostMapping("takeAccount")
    public void takeAccount(@RequestBody UserAccountFeignDto userAccountFeignDto) {
        userApplicationService.takeAccount(userAccountFeignDto.getMoney(), userAccountFeignDto.getAccount());
    }

    @ApiOperation("此方法为feign调用，查询账号余额")
    @GetMapping("accountByUserId")
    public UserAccount accountByUserId(@RequestParam("userId") Long userId) {
        return userApplicationService.accountByUserId(userId);
    }

}